package de.jmda.sample.xml;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.lang.model.element.TypeElement;

import org.junit.Test;

import de.jmda.sample.xml.p1.XMLType1;
import de.jmda.sample.xml.p2.XMLType2;

public class JUTXMLRootElementProcessor
{
	private final static File SOURCE_ROOT = new File("./src/test/java");

	@Test
	public void testProcessFile()
	{
		Set<TypeElement> set = XMLRootElementProcessor.process(SOURCE_ROOT);
		Map<String, TypeElement> map = new HashMap<String, TypeElement>();
		
		for (TypeElement typeElement : set)
		{
			map.put(typeElement.toString(), typeElement);
		}
		
		assertTrue(
				XMLType1.class.getName() + " key not found in map",
				map.keySet().contains(XMLType1.class.getName()));
		assertTrue(
				XMLType2.class.getName() + " key not found in map",
				map.keySet().contains(XMLType2.class.getName()));
		assertEquals(
				XMLType1.class.getName() + " type element not found in map",
				map.get(XMLType1.class.getName()).toString(), XMLType1.class.getName());
		assertEquals(
				XMLType2.class.getName() + " type element not found in map",
				map.get(XMLType2.class.getName()).toString(), XMLType2.class.getName());
		
		System.out.println(asString(map));
	}
	
	private String asString(Map<String, TypeElement> map)
	{
		StringBuffer result = new StringBuffer();
		
		for (String key : map.keySet())
		{
			result.append("key: " + key + " value: " + map.get(key) + "\n");
		}
		
		return result.toString();
	}
}